/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.converter;

import br.com.foxline.clinicplus.entity.Customer;
import br.com.foxline.clinicplus.service.AutocompleteService;
import br.com.foxline.clinicplus.service.CustomerService;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author roniere
 */
@ManagedBean(name="customerconverter")
@RequestScoped
public class CustomerConverter implements Converter{
    
    @EJB
    private CustomerService service;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Customer customer = new Customer();
        Long id;
        try{
            id = Long.parseLong(value);
            customer.setId(id);
        }catch(Exception e){
            customer.setName(value);
            return customer;
        }
        return service.find(customer.getId());
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Customer customer = (Customer) value;
        try{
//            customer = service.find(customer.getId());
        }catch(Exception e){
            
        }
        
        return customer.getId()+"";
    }
    
}
